package io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;

/**
 * This class provides access to UserHasConfHasMan data for the peer review system.
 * 
 * @author Lun-Wei Chang
 * @author Christopher M. Augg
 * @author James Kantak
 * 
 * @version March 9, 2014
 * 
 */
public class UserHasConfHasManAccess extends AbstractAccess {
  /** The UserHasConfHasManAccess id. */
  public static final int CM_ID = 0;
  /** The conference id. */
  public static final int CONF_ID = 1;
  /** The manuscript id. */
  public static final int MANUSCRIPT_ID = 2;
  /** The user id. */
  public static final int USER_ID = 3;
  
  /** The name of the XML file. */
  private static final String XML_FILE = "UserHasConfHasMan.xml";
  
  /** A constant for the UserHasConfHasManAccess identification attribute in the XML file. */
  private static final String NODE_USERCONFMAN_ID = "CMID";
  
  /** A constant for the UserHasConfHasManAccess identification attribute in the XML file. */
  private static final String NODE_CONF_ID = "ConfID";
  
  /** A constant for the UserHasConfHasManAccess identification attribute in the XML file. */
  private static final String NODE_MANUSCRIPT_ID = "ManuscriptID";
  
  /** A constant for the UserHasConfHasManAccess identification attribute in the XML file. */
  private static final String NODE_USER_ID = "UserID";
  
  
  /**
   * The constructor to instantiate the UserHasConfHasMan objects.
   * @throws IOException If there is a problem establishing connectivity to the file.
   * @throws JDOMException If the file cannot be correctly parsed. 
   */
  public UserHasConfHasManAccess() throws IOException, JDOMException {
    super(XML_FILE);
  }

  
  /** 
   * Returns a list of manuscripts id where each manuscript belongs to the specific user with 
   * specific role in specific conference. 
   * @param the_userid the user id.
   * @param the_confid the conference id.
   * @param the_roleid the role id.
   * @return A list of manuscript IDs.
   * */
  public List<Long> getManuscriptID(final long the_userid, final long the_confid,
                                    final long the_roleid) {


    // An array list to map the user's manuscripts.
    final List<Long> man_id_list = new ArrayList<Long>();


    final Element rootnode = getDoc().getRootElement();
    final List<Element> list =  rootnode.getChildren(NODE_USERCONFMAN_ID);

    for (int i = 0; i < list.size(); i++) {

      final Element node = (Element) list.get(i);
      
      final String conf_test = node.getChildText(NODE_CONF_ID);
      final long conf_num = Integer.parseInt(conf_test);
      
      final String user_test = node.getChildText(NODE_USER_ID);
      final long user_num = Integer.parseInt(user_test);
      
      final String role_test = node.getChildText("RID");
      final long role_num = Integer.parseInt(role_test);
     
      if (conf_num == the_confid && user_num == the_userid && role_num == the_roleid) {
          
        final String man_id = node.getChildText(NODE_MANUSCRIPT_ID);
        final long man_num = Integer.parseInt(man_id);
      
        man_id_list.add(man_num);
      }
        
    }
    
    return man_id_list;
  }
}
